//
//  BK_Calendar_WeekdayView.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/28.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Calendar_WeekdayView.h"
#import "BK_Calendar_WeekdayCell.h"
@implementation BK_Calendar_WeekdayView
{
    UICollectionView *_collectionView;
    NSMutableArray <CellModel *>*_dataArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = [NSMutableArray array];
        [self creatData];
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    _collectionView = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        collection.backgroundColor = [UIColor clearColor];
        collection.delegate = self;
        collection.dataSource = self;
        [collection registerClass:[BK_Calendar_WeekdayCell class] forCellWithReuseIdentifier:@"weekdayCell"];
        collection;
    });
    [self addSubview:_collectionView];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    _collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *collectionTopConstraint = [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    NSLayoutConstraint *collectionLeftConstraint = [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    NSLayoutConstraint *collectionRightConstraint = [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
    NSLayoutConstraint *collectionBottomConstraint = [NSLayoutConstraint constraintWithItem:_collectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
    
    [self addConstraints:@[collectionTopConstraint, collectionLeftConstraint, collectionRightConstraint, collectionBottomConstraint]];
}

- (void)creatData
{
    if (_dataArray.count) {
        [_dataArray removeAllObjects];
    }
    NSArray *array = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    for (NSString *dateStr in array) {
        
        BOOL isFrist = [dateStr isEqualToString:array.firstObject];
        BOOL isLast = [dateStr isEqualToString:array.lastObject];
        
        NSString *shouldRed = isFrist || isLast ? @"1" : @"0";
        
        CellModel *cm = [[CellModel alloc] init];
        cm.userInfo = @{@"text" : dateStr, @"shouldRed" : shouldRed};
        [_dataArray addObject:cm];
    }
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = CGRectGetWidth(self.frame);
    CGFloat height = CGRectGetHeight(self.frame);
    return CGSizeMake(width / 7.0, height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BK_Calendar_WeekdayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"weekdayCell" forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellModel *cm = _dataArray[indexPath.row];
    BK_Calendar_WeekdayCell *item = (BK_Calendar_WeekdayCell *)cell;
    [item fillData:cm.userInfo];
}

@end





















